package com.laotou99.wmsjjj.wmsjjj.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List; 
 
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laotou99.wmsjjj.wmsjjj.bean.po.ASNDetailPO;
import com.laotou99.wmsjjj.wmsjjj.bean.po.ASNPO;
import com.laotou99.wmsjjj.wmsjjj.bean.po.LOTPO;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.ASNDetailVO;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.ASNVO;
import com.laotou99.wmsjjj.wmsjjj.repository.ASNDetailPORepository;
import com.laotou99.wmsjjj.wmsjjj.repository.ASNRepository;
import com.laotou99.wmsjjj.wmsjjj.repository.LOTPORepository;
import com.laotou99.wmsjjj.wmsjjj.utils.ToolsUtils;


/**
 * 入库 service
 * */
@Service("asnService")
public class ASNService {

	@Autowired
	ASNRepository asnRepository;

	@Autowired
	ASNDetailPORepository asnDetailPORepository;
	
	@Autowired
	LOTPORepository lotpoRepository;
	
	//根据ASNid得到ASN表头信息
	public ASNVO getAsnById(long asnid) { 
		ASNPO po1 = asnRepository.findOne(asnid);
		ASNVO vo1 = null;
		if(po1==null) {
			return vo1;
		}
		vo1 = new ASNVO();
		BeanUtils.copyProperties(po1, vo1);
		System.out.println("getAsnById vo1-=>"
			+JSON.toJSONString(vo1));
		return vo1;
	}

	//保存ASN表头
	public boolean saveASN(ASNVO asnvo) { 
		ASNPO asnpo = new ASNPO(); 
		BeanUtils.copyProperties(asnvo, asnpo);
		
		asnpo.setCreateDate(new Date()); 
		ASNPO asnpo2 = asnRepository.save(asnpo);
		System.out.println("asnpo2-=>"
			+JSON.toJSONString(asnpo2));
		
		Long l1 = asnpo2.getId();
		String ASNNum1 = ToolsUtils.addZero(l1);
		
		asnpo2.setASNNum(ASNNum1);
		asnpo2.setASNID(l1.intValue());
		asnpo2.setStatus("CREATE");
		ASNPO asnpo3 = asnRepository.save(asnpo2);
		System.out.println("asnpo3-=>"
			+JSON.toJSONString(asnpo3));
		asnRepository.flush();
		return true;
	}

	//更新ASN信息
	public boolean updateASN(ASNVO asnvo) 
	{
		if(asnvo.getId()==0) {
			return false;
		}
		ASNPO asnpo = new ASNPO();	
		BeanUtils.copyProperties(asnvo, asnpo);
		ASNPO asnpo2 = asnRepository.save(asnpo);
		System.out.println("asnpo2-=>"
			+JSON.toJSONString(asnpo2));
		return true;
	}

	public List<ASNVO> getASNList() 
	{ 	
		Iterable<ASNPO> findAll = asnRepository.findAll();
		System.out.println("findAll-=>"
			+JSON.toJSONString(findAll));
		Iterator<ASNPO> iterator = findAll.iterator(); 
		//Spliterator<ASNPO> spliterator = findAll.spliterator();  
		System.out.println("iterator-=>"
			+JSON.toJSONString(iterator)); 
//		System.out.println("iterator.next()-=>"+JSON.toJSONString(iterator.next())); 
		System.out.println(iterator.hasNext());
		for(Iterator<ASNPO> iter=findAll.iterator();iter.hasNext();) {
			ASNPO asnpo = iter.next();
			System.out.println("asnpo-=>"
				+JSON.toJSONString(asnpo));
		}
//		int i1 = 0;
//		while(iterator.hasNext()) { 
//			System.out.println("iterator"+
//				JSON.toJSONString(iterator.next())+",i1-=>"+i1++);
//		} 
//		List<ASNPO> li11 = ToolsUtils.copyIterator(iterator);
//		System.out.println("li11-=>"+JSON.toJSONString(li11));   
		List<ASNPO> li12 = new ArrayList<>();
		findAll.forEach(single ->{li12.add(single);}); 
		System.out.println("li12-=>"
			+JSON.toJSONString(li12));
		List<ASNVO> li22 = new ArrayList<>(); 
		for(ASNPO po1 : li12) {
			ASNVO vo1 = new ASNVO();
			BeanUtils.copyProperties(po1, vo1);
			li22.add(vo1);
		}
		System.out.println("li22-=>"
			+JSON.toJSONString(li22));
		return li22;
	}
/*
 for (Iterator iter = l.iterator(); iter.hasNext();) {
     String str = (String)iter.next();
     System.out.println(str);
}
Iterable<Entity> geted = entityDao.findAll();  
List<Entity> list = Lists.newArrays();  
geted.forEach(single ->{list.add(single)});  
 * */

	//根据ID-保存明细信息
	public boolean saveASNDetail(ASNDetailVO asndvo, long id1) 
	{	
		if(checkASN_ID(id1)) {
			return false;
		}
		System.out.println("asndvo-=>"
			+JSON.toJSONString(asndvo));
		ASNDetailPO asndpo = new ASNDetailPO();  
		BeanUtils.copyProperties(asndvo, asndpo);
		
		asndpo.setId(id1);
		asndpo.setCreateDate(new Date());
		asndpo.setStatus("CREATE");
		ASNDetailPO detailPO = 
			asnDetailPORepository.save(asndpo);
		System.out.println("detailPO-=>"
			+JSON.toJSONString(detailPO));
		
		return true;
	}

	private boolean checkASN_ID(long id1) 
	{ 	
		ASNPO findOne = asnRepository.findOne(id1);
		if(findOne==null) {
			return true;
		}
		System.out.println("findOne-=>"+JSON.toJSONString(findOne));
		return false;
	}

	public List<ASNDetailVO> getASNDetailList(long id1) 
	{
		List<ASNDetailVO> list_asndvo = new ArrayList<ASNDetailVO>();
		List<ASNDetailPO> list_asnd1 = asnDetailPORepository.findById(id1);
		System.out.println("list_asnd1-=>"
			+JSON.toJSONString(list_asnd1));
		int i1 = 1;
		for(ASNDetailPO asndpo:list_asnd1) {
			ASNDetailVO asndvo = new ASNDetailVO();
			BeanUtils.copyProperties(asndpo, asndvo);
			asndvo.setSeq(i1++);
			list_asndvo.add(asndvo);
		}  
		return list_asndvo;
	}	
	
	public void test1() {}

	/**
	 * @category 收货操作
	 * */
	public String receiveASN(Long id1) { 
		System.out.println(
			"receiveASN id1-=>"+id1);
		
		if(checkID_isReceived(id1)) {
			return "已收货不可再收货";
		}
		if(checkID_locIsNull(id1)) {
			return "收货库位未填全，不可收货";
		} 
		
		ASNPO asnpo1 = asnRepository.findOne(id1);
		
		List<ASNDetailPO> li1 = asnDetailPORepository.findById(id1);
		for(ASNDetailPO po1:li1) {
			System.out.println("po1-=>"+JSON.toJSONString(po1));
			LOTPO po2 = new LOTPO();
			po2.setASNNum(asnpo1.getASNNum());
			po2.setLegalUnit(po1.getLegalUnit());
			po2.setMoqNum(po1.getMoqNum());
			po2.setLocID(po1.getLocID());
			po2.setSku(po1.getSku());
			po2.setStoreKey(asnpo1.getStorekey());
			
			LOTPO lotpo3 = lotpoRepository.save(po2);
			System.out.println("lotpo3-=>"
				+JSON.toJSONString(lotpo3));
			String lotid1 = ""+lotpo3.getId();
			lotpo3.setLotID(lotid1);
			
			LOTPO lotpo4 = lotpoRepository.save(lotpo3);
			System.out.println("lotpo4-=>"
				+JSON.toJSONString(lotpo4));
			
			po1.setLotID(lotid1);
			po1.setStatus("RECEIVED");
			ASNDetailPO dPO5 = 
				asnDetailPORepository.save(po1);
			
			System.out.println("dPO5-=>"
				+JSON.toJSONString(dPO5));
		}  
		asnpo1.setStatus("RECEIVED");
		asnpo1.setReceiveDate(new Date());
		ASNPO asnpo2 = asnRepository.save(asnpo1);
		System.out.println("asnpo2-=>"
			+JSON.toJSONString(asnpo2));
		return "收货完成";
	}

	private boolean checkID_locIsNull(Long id1) { 
		
		List<ASNDetailPO> li1 = 
			asnDetailPORepository.findById(id1);
		for(ASNDetailPO po: li1) {
			if(po.getLocID()==null || 
				po.getLocID().equals("")) 
			{
				return true;
			}
		} 
		return false;
	}

	private boolean checkID_isReceived(Long id1) {
		
		ASNPO po1 = asnRepository.findOne(id1);
		
		if(po1.getStatus().equals("RECEIVED")) 
		{
			return true;
		} 
		return false;
	}

	//更新详细明细
	public boolean updateASNDetail(ASNDetailVO vo) {
		System.out.println("updateASNDetail vo-=>"
			+JSON.toJSONString(vo));
		if(vo.getDid()==0) {
			return true;
		} 
		ASNDetailPO po = new ASNDetailPO();
		BeanUtils.copyProperties(vo, po);
		
		ASNDetailPO detailPO = 
			asnDetailPORepository.save(po);
		System.out.println("detailPO-=>"
			+JSON.toJSONString(detailPO));
 
		return false;
	}

	public String showList(JSONObject jo1) {
		System.out.println("ASN showList()");
		
		List<ASNPO> findAll = asnRepository.findAll();
		Collections.reverse(findAll);
		
		return JSON.toJSONString(findAll);
	}

	public String addNew(JSONObject jo1) {
		System.out.println("jo1-=>"+jo1.toJSONString());
		ASNPO ap1 = jo1.toJavaObject(ASNPO.class);
		ap1.setCreateDate(new Date());
		ASNPO ap2 = asnRepository.save(ap1);
		System.out.println("ap2-=>"+JSON.toJSONString(ap2));

		Long l1 = ap2.getId();
		String ASNNum1 = ToolsUtils.addZero(l1);
		ap2.setASNNum(ASNNum1);
		ap2.setASNID(l1.intValue());
		ap2.setStatus("CREATE");
		ASNPO ap3 = asnRepository.save(ap1);
		System.out.println("ap3-=>"+JSON.toJSONString(ap3));
		asnRepository.flush();
		
		return "id:"+ap2.getId();
	}

	public String selectById(JSONObject jo1) { 
		System.out.println("jo1-=>"+jo1.toJSONString());
		Long lng1 = jo1.getLong("id");
		
		ASNPO findOne = asnRepository.findOne(lng1);
		return JSON.toJSONString(findOne);
	}

	public String update(JSONObject jo1) {
		System.out.println("update jo1-=>"+jo1.toJSONString()); 
		
		ASNPO ap1 = jo1.toJavaObject(ASNPO.class);
		ASNPO ap2 = asnRepository.save(ap1);
		System.out.println("ap2-=>"+JSON.toJSONString(ap2));
		return "OK";
	}

	public String delete(JSONObject jo1) {
		System.out.println("delete jo1"+jo1.toJSONString());  
		Long lng1 = jo1.getLong("id");
		asnRepository.delete(lng1);
		return "OK";
	}

	public String showDetailList(JSONObject jo1) { 
		System.out.println("ASN showList()");
		
		List<ASNDetailPO> findAll = asnDetailPORepository.findAll();
		Collections.reverse(findAll);
		
		return JSON.toJSONString(findAll);
	}

	public String selectDetailById(JSONObject jo1) {
		System.out.println("jo1"+jo1.toJSONString());
		Long lng1 = jo1.getLong("id");
		
//		ASNDetailPO ap1 = new ASNDetailPO();
//		ap1.setId(lng1);
//		Example<ASNDetailPO> example = Example.of(ap1);
		List<ASNDetailPO> list1 = asnDetailPORepository.findById(lng1);
		//List<ASNDetailPO> list2 = asnDetailPORepository.findById2(lng1);
		//List<ASNDetailPO> list1 = asnDetailPORepository.findAll(example);
		Collections.reverse(list1);
		
		return JSON.toJSONString(list1);
	}

	public String addNewDetail(JSONObject jo1) {
		System.out.println("addNewDetail jo1-=>"+jo1.toJSONString());
		
		ASNDetailPO adp1 = jo1.toJavaObject(ASNDetailPO.class);
		adp1.setCreateDate(new Date());
		adp1.setStatus("CREATE");
		ASNDetailPO adp2 = asnDetailPORepository.save(adp1);
		System.out.println("addNewDetail adp2-=>"+JSON.toJSONString(adp2));
		return JSON.toJSONString(adp2);
	}

	public String selectDetailByDid(JSONObject jo1) {
		System.out.println("jo1-=>"+jo1.toJSONString()); 
		Long lng1 = jo1.getLong("did");
		
		ASNDetailPO findOne = asnDetailPORepository.findOne(lng1);
		return JSON.toJSONString(findOne);
	}

	public String updateDetail(JSONObject jo1) {
		System.out.println("update jo1-=>"+jo1.toJSONString());  
		ASNDetailPO adp1 = jo1.toJavaObject(ASNDetailPO.class);
		adp1.setModifyDate(new Date());
		ASNDetailPO adp2 = asnDetailPORepository.save(adp1);
		return JSON.toJSONString(adp2);
	}

	public String deleteDetail(JSONObject jo1) {
		System.out.println("jo1"+jo1.toJSONString()); 
		Long lng1 = jo1.getLong("did");
		asnDetailPORepository.delete(lng1);
		return "OK";
	}

	//完成收货
	public String finishASNReceive(JSONObject jo1) {
		System.out.println("jo1"+jo1.toJSONString()); 
		Long lng1 = jo1.getLong("id");
		List<ASNDetailPO> list1 = asnDetailPORepository.findById(lng1);
		for (ASNDetailPO asnDetailPO : list1) {
			String locID = asnDetailPO.getLocID();
			System.out.println("locID-=>"+locID);
			if(locID == "" || locID == null || locID.isEmpty()) {
				return "{'msg':'库位未分配'}";
			}
		}
		
		ASNPO asnpo1 = asnRepository.findOne(lng1);
		System.out.println("asnpo1-=>"+JSON.toJSONString(asnpo1));
		String asnNum = asnpo1.getASNNum(); 

		for (ASNDetailPO asnDetailPO : list1) {
			LOTPO lotpo = new LOTPO();
			lotpo.setASNNum(asnDetailPO.getASNNum());
			lotpo.setSku(asnDetailPO.getSku());
			lotpo.setStoreKey(asnpo1.getStorekey());
			lotpo.setMoqNum(asnDetailPO.getMoqNum());
			lotpo.setLegalUnit(asnDetailPO.getLegalUnit());
			lotpo.setLocID(asnDetailPO.getLocID());
			lotpo.setRemark1(asnDetailPO.getRemark1());
			lotpo.setRemark2(asnDetailPO.getRemark2());
			lotpo.setRemark3(asnDetailPO.getRemark2());
			lotpo.setStatus("NORMAL");
			
			asnDetailPO.setStatus("FINISH");
			ASNDetailPO adp1 = asnDetailPORepository.save(asnDetailPO);
			System.out.println("adp1--=>"+JSON.toJSONString(adp1));
			
			LOTPO lotpo2 = lotpoRepository.save(lotpo);
			System.out.println("lotpo2-=>"+JSON.toJSONString(lotpo2));
			String lotID = ToolsUtils.addLotZero(lotpo2.getId());
			lotpo2.setLotID(lotID);
			LOTPO lotpo3 = lotpoRepository.save(lotpo2);
			System.out.println("lotpo2-=>"+JSON.toJSONString(lotpo3));
		}
		
		asnpo1.setStatus("FINISH");
		
		ASNPO asnpo2 = asnRepository.save(asnpo1);
		System.out.println("asnpo2-=>"+JSON.toJSONString(asnpo2));
		
		return "OK";
	} 


	public String findASNbyForm(JSONObject jo1){
		System.out.println("findASNbyForm");
		System.out.println("jo1-=>"+jo1.toJSONString());

		ASNPO asnpo = new ASNPO();
		ASNPO asnpo1 = jo1.toJavaObject(ASNPO.class);
		System.out.println("asnpo1-=>"+JSON.toJSONString(asnpo1));

		Example<ASNPO> example = Example.of(asnpo1);
		List<ASNPO> findAll = asnRepository.findAll(example);
		
		System.out.println("findAll-=>"+JSON.toJSONString(findAll));
		return JSON.toJSONString(findAll);
	}
}
